//
//  jumpViewController.m
//  bgCar
//
//  Created by cckv on 2017/9/25.
//  Copyright © 2017年 kv. All rights reserved.
//


#import "jumpViewController.h"
#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>

#import "webViewController.h"


@interface jumpViewController ()

@end

@implementation jumpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self reachability];
    
}
- (void)reachability
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
            {
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"没有网络连接", nil) message:NSLocalizedString(@"请检查您的网络是否正常", nil) preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self reachability];
                }];
                [alertVc addAction:action];
                [self presentViewController:alertVc animated:YES completion:nil];

            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");
                
                [self getData];
                
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
                [self getData];
                
                break;
        }
    }];
    // 3.开始监控
    [mgr startMonitoring];
}

- (void)getData
{
    [SVProgressHUD showWithStatus:NSLocalizedString(@"加载中。。。", nil)];
    AVQuery *query = [AVQuery queryWithClassName:@"myData"];
    [query getObjectInBackgroundWithId:@"59c8b2d91b69e6004034df9f" block:^(AVObject *object, NSError *error) {
        
        [SVProgressHUD dismiss];
        
        BOOL so = [[object objectForKey:@"show"] boolValue];
        NSString *str = [object objectForKey:@"url"];
        if (so) {
            NSLog(@"%@",str);
            webViewController *webVc = [webViewController new];
            webVc.urlStr = str;
            [UIApplication sharedApplication].keyWindow.rootViewController = webVc;
        }else{
            [UIApplication sharedApplication].keyWindow.rootViewController = [welcomeViewController new];
        }
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
